<template>
  <div class="goods_list">
    <ul>
      <li v-for="item in goodsData" :key="item.product_id">
        <el-popover placement="top">
          <p>确定删除吗？</p>
          <div style="text-align: right; margin: 10px 0 0">
            <el-button type="primary" size="mini" @click="delCollect(item.product_id)">确定</el-button>
          </div>
          <i class="el-icon-close delete" slot="reference" v-show="isDelete"></i>
        </el-popover>
        <router-link :to="{path:'detail',query:{productID: item.product_id}}">
          <img :src="$target +item.product_picture" :alt="item.product_name" />
          <h2>{{item.product_name}}</h2>
          <h3>{{item.product_title}}</h3>
          <p>
            <span>{{item.product_selling_price}}元</span>
            <span
              class="del"
              v-show="item.product_price != item.product_selling_price"
            >{{item.product_price}}元</span>
          </p>
        </router-link>
      </li>
      <li v-show="isMore " id="more" @click="check_more">
        <router-link to>
          浏览更多
          <i class="el-icon-d-arrow-right"></i>
        </router-link>
      </li>
    </ul>
  </div>
</template>

<script>
import detailAPI from "@/request/api/detailAPI";
export default {
  name: "GoodsList",
  props: ["goodsData", "isMore", "isDelete"],
  created() {
    console.log(this.goodsData);
  },
  computed: {
    categoryArr() {
      let arr = [];
      // 遍历当前每一条数据
      this.goodsData.forEach((item) => {
        // 数组去重，数据分类id在arr中存在不执行push
        if (!arr.includes(item.category_id)) {
          // 商品分类id放入arr中
          arr.push(item.category_id);
        }
      });
      return arr;
    },
  },
  methods: {
    // 点击浏览更多
    check_more() {
      console.log("11111", this.categoryArr, this.$categoryArr);
      this.$globalData.$categoryArr = this.categoryArr;
      this.$router.push({
        path: "/goods",
        query: {
          categoryID: this.categoryArr,
        },
      });
    },
    // 点击删除
    async delCollect(product_id) {
      let { data: res } = await detailAPI.getdeleteCollect({
        user_id: this.$store.getters.getUser.user_id,
        product_id: product_id,
      });
      if (res.code == "001") {
        // 后端删除,前端页面删除
        for (let i = 0; i < this.goodsData.length; i++) {
          if (this.goodsData[i].product_id == product_id) {
            this.goodsData.splice(i, 1);
          }
        }
        this.$notify({
          title: "删除成功",
          message: res.msg,
          type: "success",
        });
      } else {
        this.$notify.error({
          title: "删除失败",
          message: res.msg,
        });
      }
    },
  },
};
</script>

<style scoped lang="scss">
.goods_list ul li {
  z-index: 1;
  float: left;
  width: 234px;
  height: 280px;
  padding: 10px 0;
  margin: 0 0 14.5px 13.7px;
  background-color: white;
  -webkit-transition: all 0.2s linear;
  transition: all 0.2s linear;
  position: relative;
}
.goods_list ul li:hover {
  z-index: 2;
  -webkit-box-shadow: 0 15px 30px rgba(0, 0, 0, 0.1);
  box-shadow: 0 15px 30px rgba(0, 0, 0, 0.1);
  -webkit-transform: translate3d(0, -2px, 0);
  transform: translate3d(0, -2px, 0);
}
.goods_list ul li img {
  display: block;
  width: 160px;
  height: 160px;
  background: url(../assets/imgs/placeholder.png) no-repeat 50%;
  margin: 0 auto;
}
.goods_list ul li h2 {
  margin: 25px 10px 0;
  font-size: 14px;
  font-weight: 400;
  color: #333;
  text-align: center;
  text-overflow: ellipsis;
  white-space: nowrap;
  overflow: hidden;
}
.goods_list ul li h3 {
  margin: 5px 10px;
  height: 18px;
  font-size: 12px;
  font-weight: 400;
  color: #b0b0b0;
  text-align: center;
  text-overflow: ellipsis;
  white-space: nowrap;
  overflow: hidden;
}
.goods_list ul li p {
  margin: 10px 10px 10px;
  text-align: center;
  color: #ff6700;
}
.goods_list ul li p .del {
  margin-left: 0.5em;
  color: #b0b0b0;
  text-decoration: line-through;
}
.goods_list #more {
  text-align: center;
  line-height: 280px;
}
.goods_list #more a {
  font-size: 18px;
  color: #333;
}
.goods_list #more a:hover {
  color: #ff6700;
}
.goods_list ul li .delete {
  position: absolute;
  top: 10px;
  right: 10px;
  display: none;
}
.goods_list ul li:hover .delete {
  display: block;
}
.goods_list ul li .delete:hover {
  color: #ff6700;
}
</style>